const app = getApp()
let qCloud = require("../../../index.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,
        enterpriseLogo: null,
        enterpriseLogoUrl: null,
        organName: null,
        enterpriseId: null,
        legalName: null,
        enterprisePhone: null,
        enterpriseEmail: null,
        officialWebsite: null,
        businessAddress: null,
    },

    onLoad: function (options) {
        let self = this;
        let userInfo = qCloud.session.getUser();
        self.setData({
            organName: userInfo.organName
        });

        self.getEnterpriseInfo();

    },

    chooseImage() {
        let self = this;
        wx.chooseMedia({
            count: 1,
            mediaType: ["image"],
            sourceType: ["album", "camera"],
            maxDuration: 30,
            camera: "back",
            success(res) {
                wx.uploadFile({
                    url: qCloud.api.uploadFile, //仅为示例，非真实的接口地址
                    filePath: res.tempFiles[0].tempFilePath,
                    name: "file",
                    formData: {
                        "file": res.tempFiles[0].tempFilePath,
                        "fileType": "6"
                    },
                    header: qCloud.buildSessionHeader(),
                    success(response) {
                        let resJson = JSON.parse(response.data);
                        if (resJson.code == 200) {
                            self.setData({
                                enterpriseLogo: resJson.data.filesId,
                                enterpriseLogoUrl: resJson.data.filesUrl,
                            });
                        }
                    },

                    fail(error) {
                        wx.showToast({
                            title: error,
                            icon: "error"
                        })
                    }
                })
            },
            fail(e) {

            }
        });
    },


    legalNameInput: function (e) {
        this.setData({
            legalName: e.detail.value
        })
    },

    enterprisePhoneInput: function (e) {
        this.setData({
            enterprisePhone: e.detail.value
        })
    },

    enterpriseEmailInput: function (e) {
        this.setData({
            enterpriseEmail: e.detail.value
        })
    },

    officialWebsiteInput: function (e) {
        this.setData({
            officialWebsite: e.detail.value
        })
    },

    businessAddressInput: function (e) {
        this.setData({
            businessAddress: e.detail.value
        })
    },

    getEnterpriseInfo: function () {
        let self = this;
        app.fetchApis(self, qCloud.api.getEnterpriseInfo, {}, "GET", function (e) {
            let result = e.data;
            if (result.code == 200) {
                self.setData({
                    enterpriseId: result.data.enterpriseId,
                    enterpriseLogo: result.data.enterpriseLogo,
                    enterpriseLogoUrl: result.data.enterpriseLogoUrl,
                    legalName: result.data.legalName,
                    enterprisePhone: result.data.enterprisePhone,
                    enterpriseEmail: result.data.enterpriseEmail,
                    officialWebsite: result.data.officialWebsite,
                    businessAddress: result.data.businessAddress,
                })
            }
        });
    },

    submit: function () {
        let self = this;
        if(self.data.enterprisePhone == null || self.data.enterprisePhone == ""){
            wx.showToast({
                title: "客服电话不能为空"
            })
            return
        }

        let params = {
            enterpriseId: self.data.enterpriseId,
            enterpriseLogo: self.data.enterpriseLogo,
            legalName: self.data.legalName,
            enterprisePhone: self.data.enterprisePhone,
            enterpriseEmail: self.data.enterpriseEmail,
            officialWebsite: self.data.officialWebsite,
            businessAddress: self.data.businessAddress
        }

        app.fetchApis(self, qCloud.api.editEnterpriseInfo, params, "POST", function (e) {
            let result = e.data;
            if (result.code == 200) {
                wx.showToast({
                    title: "保存成功",
                    icon: "success"
                })
            }else{
                wx.showToast({
                    title: "保存失败",
                    icon: "success"
                })
            }
        });
    },


})
